package it.getta.vicinometro;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;

import it.getta.vicinometro.model.PlaceList;
import it.getta.vicinometro.popup.VicinometroPopup;
import it.getta.vicinometro.popup.WarningPopup;
import it.getta.vicinometro.popup.WarningPopup.OnPopupClickListener;
import it.getta.vicinometro.tracking.TrackingTask;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class SettingsActivity extends Activity implements ActivityCallBack {
	
	public static final String SETTINGS_PREFERENCES = "it.getta.vicinometro.SETTINGS_PREFERENCES";
	
	public static final int PRECISION_VERY_COARSE = 0;
	public static final int PRECISION_COARSE = 1;
	public static final int PRECISION_GOOD = 2;
	public static final int PRECISION_MAX = 3;
	
	public static final String KEY_NEAREST_PLACES_DISTANCE = "it.getta.vicinometro.KEY_NEAREST_PLACES_DISTANCE";
	public static final String KEY_MAX_INTEREST_PLACES = "it.getta.vicinometro.KEY_MAX_INTEREST_PLACES";
	public static final String KEY_MAX_INTEREST_PLACES_IMAGES = "it.getta.vicinometro.KEY_MAX_INTEREST_PLACES_IMAGES";
	public static final String KEY_GPS_TIMEOUT = "it.getta.vicinometro.KEY_GPS_TIMEOUT";
	public static final String KEY_LOCATION_ACCURACY = "it.getta.vicinometro.KEY_LOCATION_ACCURACY";
	public static final String KEY_ALARM_TONE = "it.getta.vicinometro.KEY_ALARM_TONE";
	public static final String KEY_SHOW_TIPS = "it.getta.vicinometro.KEY_SHOW_TIPS";
	
	public static final int DEFAULT_NEAREST_PLACES_DISTANCE = 500; //meters
	public static final int DEFAULT_MAX_INTEREST_PLACES = 15;
	public static final int DEFAULT_MAX_INTEREST_PLACES_IMAGES = 15;
	public static final int DEFAULT_GPS_TIMEOUT = 30; //seconds
	public static final int DEFAULT_LOCATION_ACURACY = 2;
	public static final boolean DEFAULT_SHOW_TIPS = true;
	
	private TextView tvNearestDistance;
	private TextView tvMaxInterestPlaces;
	private TextView tvMaxInterestPlacesImages;
	private TextView tvGpsTimeout;
	
	private WarningPopup mWarningPopup;
	private LinearLayout mRootContainer;
	private SeekBar mAccuracySeek;
	private CheckBox mTipsCheck;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		mRootContainer = (LinearLayout)findViewById(R.id.settings_root_container);
		tvNearestDistance = (TextView)findViewById(R.id.tv_nearest_places_distance);
		tvMaxInterestPlaces = (TextView)findViewById(R.id.tv_max_interest_places);
		tvMaxInterestPlacesImages = (TextView)findViewById(R.id.tv_max_interest_places_images);
		tvGpsTimeout = (TextView)findViewById(R.id.tv_max_gps_timeout);
		ImageButton revertButton = (ImageButton)findViewById(R.id.settings_revert_button);
		revertButton.requestFocus();
		revertButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				showWarningPopup(R.string.warning, R.string.restore_default_settings_alert, new OnPopupClickListener() {
					
					@Override
					public void onClick() {
						restoreDefaultValues();
						mWarningPopup.dismiss();
					}
				}, 
				new OnPopupClickListener() {
					
					@Override
					public void onClick() {
						mWarningPopup.dismiss();
					}
				});
			}
		});
		final TextView veryCoarse = (TextView)findViewById(R.id.seek_very_coarse_value);
		final TextView coarse = (TextView)findViewById(R.id.seek_coarse_value);
		final TextView good = (TextView)findViewById(R.id.seek_good_value);
		final TextView max = (TextView)findViewById(R.id.seek_max_value);
		mAccuracySeek = (SeekBar)findViewById(R.id.seek_bar);
		
		mAccuracySeek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				Log.d("TAG", "progress =  " + progress); 
				switch (progress) {
				case PRECISION_VERY_COARSE:
					selectValue(veryCoarse);
					unSelectValue(coarse);
					unSelectValue(good);
					unSelectValue(max);
					break;
					
				case PRECISION_COARSE:
					selectValue(coarse);
					unSelectValue(veryCoarse);
					unSelectValue(good);
					unSelectValue(max);	
					break;
				case PRECISION_GOOD:
					selectValue(good);
					unSelectValue(coarse);
					unSelectValue(veryCoarse);
					unSelectValue(max);
					break;
				case PRECISION_MAX:
					selectValue(max);
					unSelectValue(coarse);
					unSelectValue(good);
					unSelectValue(veryCoarse);
					break;

				default:
					break;
				}
			}
		});
		
		mTipsCheck = (CheckBox)findViewById(R.id.settings_check_tips);
		
		readSharedPreferences();
	}
	
	@Override
	protected void onStop() {
		super.onStop();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		saveSharedPreferences();
	}
	
	private void saveSharedPreferences() {
		SharedPreferences prefs = getSharedPreferences(SETTINGS_PREFERENCES, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = prefs.edit();
		
		editor.putInt(KEY_NEAREST_PLACES_DISTANCE, Integer.parseInt(tvNearestDistance.getText().toString()));
		editor.putInt(KEY_MAX_INTEREST_PLACES, Integer.parseInt(tvMaxInterestPlaces.getText().toString()));
		editor.putInt(KEY_MAX_INTEREST_PLACES_IMAGES, Integer.parseInt(tvMaxInterestPlacesImages.getText().toString()));
		editor.putInt(KEY_GPS_TIMEOUT, Integer.parseInt(tvGpsTimeout.getText().toString()));
		editor.putInt(KEY_LOCATION_ACCURACY, mAccuracySeek.getProgress());
		editor.putBoolean(KEY_SHOW_TIPS, mTipsCheck.isChecked());
		
		editor.commit();
	}
	
	private void readSharedPreferences() {
		SharedPreferences prefs = getSharedPreferences(SETTINGS_PREFERENCES, Context.MODE_PRIVATE);
		tvNearestDistance.setText("" + prefs.getInt(KEY_NEAREST_PLACES_DISTANCE, DEFAULT_NEAREST_PLACES_DISTANCE));
		tvMaxInterestPlaces.setText("" + prefs.getInt(KEY_MAX_INTEREST_PLACES, DEFAULT_MAX_INTEREST_PLACES));
		tvMaxInterestPlacesImages.setText("" + prefs.getInt(KEY_MAX_INTEREST_PLACES_IMAGES, DEFAULT_MAX_INTEREST_PLACES_IMAGES));
		tvGpsTimeout.setText("" + prefs.getInt(KEY_GPS_TIMEOUT, DEFAULT_GPS_TIMEOUT));
		
		int accuracy = prefs.getInt(KEY_LOCATION_ACCURACY, DEFAULT_LOCATION_ACURACY);
		mAccuracySeek.setProgress(accuracy);
		
		mTipsCheck.setChecked(prefs.getBoolean(KEY_SHOW_TIPS, DEFAULT_SHOW_TIPS));
	}
	
	private void restoreDefaultValues() {
		tvNearestDistance.setText("" + DEFAULT_NEAREST_PLACES_DISTANCE);
		tvMaxInterestPlaces.setText("" + DEFAULT_MAX_INTEREST_PLACES);
		tvMaxInterestPlacesImages.setText("" + DEFAULT_MAX_INTEREST_PLACES_IMAGES);
		tvGpsTimeout.setText("" + DEFAULT_GPS_TIMEOUT);
		mAccuracySeek.setProgress(DEFAULT_LOCATION_ACURACY);
		mTipsCheck.setChecked(DEFAULT_SHOW_TIPS);
	}
	
	private void selectValue(TextView tv) {
		tv.setTextAppearance(this, R.style.seekHighlightedText);
	}
	
	private void unSelectValue(TextView tv) {
		tv.setTextAppearance(this, R.style.seekNormalText);
	}
	
	@Override
	public void showWarningPopup(int titleResId, int messageResId, OnPopupClickListener positiveListener, OnPopupClickListener negativeListener) {
		mWarningPopup = new WarningPopup(this, this);
		mWarningPopup.setTitle(titleResId);
		mWarningPopup.setMessage(messageResId);
		mWarningPopup.setOnPositiveButtonListener(positiveListener);
		mWarningPopup.setOnNegativeButtonListener(negativeListener);
		mWarningPopup.setBackgroundStyle(WarningPopup.BKG_NOT_TRASPARENT);
		mWarningPopup.showPopup();
	}

	@Override
	public void showOptionsPopup(GeoPoint pointSelected) {
		
	}

	@Override
	public void showListPopup(GeoPoint pointSelected) {
		
	}

	@Override
	public void showInfoPopup(String reference) {
		
	}

	@Override
	public void showAsyncLoading() {
		
	}

	@Override
	public void showSetAlarmPopup(GeoPoint pointSelected) {
		
	}

	@Override
	public void showAlarmPointPopup() {
		
	}

	@Override
	public void dismissWarningPopup() {
		
	}

	@Override
	public void hideAsyncLoading() {
		
	}

	@Override
	public View getPopupAnchor() {
		return mRootContainer;
	}

	@Override
	public void addAlarmMarker(GeoPoint point, float radius,
			boolean removePrevious) {
		
	}

	@Override
	public MapView getMapView() {
		return null;
	}

	@Override
	public void removeAlarmMarker() {
		
	}

	@Override
	public void showWhereIAmPopup() {
		
	}

	@Override
	public TrackingTask getTrackingTask() {
		return null;
	}

	@Override
	public void showAlert(int titleResId, int messageResId) {
		
	}

	@Override
	public void showListPopup(GeoPoint pointSelected, PlaceList chachedPlaces) {
		
	}

	@Override
	public void fillCachedPlaces(PlaceList chachedPlaces) {
		
	}

	@Override
	public PlaceList getCachedPlaces() {
		return null;
	}

	@Override
	public void showNetworkAlert() {
		
	}

	@Override
	public void startFullscreenGallery(String[] data, int selectedPosition) {
		
	}

	@Override
	public void popupShown(VicinometroPopup popup) {
		
	}
}
